home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / MotifTabbedPaneUI.java < prev    next >
Text File  |  1998-06-30  |  12KB  |  322 lines

  1. /*
  2.  * @(#)MotifTabbedPaneUI.java    1.36 98/02/02
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.motif;
  22.  
  23. import java.awt.*;
  24. import java.awt.event.*;
  25. import com.sun.java.swing.*;
  26. import com.sun.java.swing.event.*;
  27. import com.sun.java.swing.plaf.*;
  28. import com.sun.java.swing.plaf.basic.BasicTabbedPaneUI;
  29. import java.io.Serializable; 
  30.  
  31. /**
  32.  * A Motif L&F implementation of TabbedPaneUI.
  33.  * <p>
  34.  * Warning: serialized objects of this class will not be compatible with
  35.  * future swing releases.  The current serialization support is appropriate
  36.  * for short term storage or RMI between Swing1.0 applications.  It will
  37.  * not be possible to load serialized Swing1.0 objects with future releases
  38.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  39.  * baseline for the serialized form of Swing objects.
  40.  *
  41.  * @version 1.36 02/02/98
  42.  * @author Dave Moore
  43.  * @author Philip Milne
  44.  * @author Amy Fowler
  45.  */
  46. public class MotifTabbedPaneUI extends BasicTabbedPaneUI
  47. {
  48.  
  49. // Class variables
  50.  
  51.     protected static int spacingHeight = 1;
  52.     protected static int spacingWidth = 3;
  53.     protected static int selectedTabHeightPad = 4;
  54.     protected static int selectedTabWidthPad = 0;
  55.     protected static Insets mContentBorderInsets = new Insets(2,2,2,2);
  56.     protected static Insets mTabsOnTopTabAreaInsets = new Insets(4,2,0,8);
  57.     protected static Insets mTabsOnLeftTabAreaInsets = new Insets(2,4,8,0);
  58.     protected static Insets mTabsOnBottomTabAreaInsets = new Insets(0,2,4,8);
  59.     protected static Insets mTabsOnRightTabAreaInsets = new Insets(2,0,8,4);
  60.  
  61. // Instance variables
  62.  
  63.     protected Color unselectedTabBackground;
  64.     protected Color unselectedTabForeground;
  65.     protected Color unselectedTabShadow;
  66.     protected Color unselectedTabHighlight;
  67.  
  68.  
  69. // UI creation
  70.  
  71.     public static ComponentUI createUI(JComponent tabbedPane) {
  72.         return new MotifTabbedPaneUI();
  73.     }
  74.  
  75.  
  76. // UI Installation/De-installation
  77.  
  78.     public void installUI(JComponent c) {
  79.         super.installUI(c);
  80.         c.setLayout(this);
  81.     }
  82.  
  83.     public void uninstallUI(JComponent c) {
  84.         super.uninstallUI(c);
  85.     }
  86.  
  87.     protected void installDefaults(JComponent c) {
  88.         LookAndFeel.installColorsAndFont(c, "TabbedPane.tabBackground",
  89.                                     "TabbedPane.tabForeground", "TabbedPane.font");  
  90.         tabShadow = UIManager.getColor("TabbedPane.tabShadow");
  91.         tabDarkShadow = tabShadow;
  92.         tabHighlight = UIManager.getColor("TabbedPane.tabHighlight");
  93.  
  94.         unselectedTabBackground = UIManager.getColor("TabbedPane.unselectedTabBackground");
  95.         unselectedTabForeground = UIManager.getColor("TabbedPane.unselectedTabForeground");
  96.         unselectedTabShadow = UIManager.getColor("TabbedPane.unselectedTabShadow");
  97.         unselectedTabHighlight = UIManager.getColor("TabbedPane.unselectedTabHighlight");
  98.         focus = UIManager.getColor("TabbedPane.focus");
  99.     }
  100.  
  101. // UI Rendering
  102.  
  103.    protected void paintContentBorderTopEdge(Graphics g, int tabPlacement,
  104.                                             int selectedIndex, 
  105.                                             int x, int y, int w, int h) {
  106.  
  107.         g.setColor(tabHighlight);
  108.         if (tabPlacement != TOP || selectedIndex < 0) {
  109.             g.drawLine(x, y, x+w-2, y);
  110.         } else {
  111.             Rectangle selRect = rects[selectedIndex];
  112.  
  113.             g.drawLine(x, y, selRect.x - 1, y);
  114.             if (selRect.x + selRect.width < x + w - 2) {
  115.                 g.drawLine(selRect.x + selRect.width, y, 
  116.                            x+w-2, y);
  117.             } 
  118.         }
  119.     }
  120.  
  121.     protected void paintContentBorderBottomEdge(Graphics g, int tabPlacement,
  122.                                                int selectedIndex,
  123.                                                int x, int y, int w, int h) { 
  124.         g.setColor(tabShadow);
  125.         if (tabPlacement != BOTTOM || selectedIndex < 0) {
  126.             g.drawLine(x+1, y+h-1, x+w-1, y+h-1);
  127.         } else {
  128.             Rectangle selRect = rects[selectedIndex];
  129.  
  130.             g.drawLine(x+1, y+h-1, selRect.x - 1, y+h-1);
  131.             if (selRect.x + selRect.width < x + w - 2) {
  132.                 g.drawLine(selRect.x + selRect.width, y+h-1, x+w-2, y+h-1);
  133.             } 
  134.         }
  135.     }
  136.  
  137.     protected void paintContentBorderRightEdge(Graphics g, int tabPlacement,
  138.                                                int selectedIndex,
  139.                                                int x, int y, int w, int h) { 
  140.  
  141.         g.setColor(tabShadow);
  142.         if (tabPlacement != RIGHT || selectedIndex < 0) {
  143.             g.drawLine(x+w-1, y+1, x+w-1, y+h-1);
  144.         } else {
  145.             Rectangle selRect = rects[selectedIndex];
  146.  
  147.             g.drawLine(x+w-1, y+1, x+w-1, selRect.y - 1);
  148.  
  149.             if (selRect.y + selRect.height < y + h - 2 ) {
  150.                 g.drawLine(x+w-1, selRect.y + selRect.height, 
  151.                            x+w-1, y+h-2);
  152.             } 
  153.         }
  154.     }
  155.  
  156.     protected void paintTabBackground(Graphics g, JTabbedPane pane, 
  157.                                       int tabPlacement, int tabIndex,
  158.                                       int x, int y, int w, int h,
  159.                        boolean isSelected ) {
  160.         g.setColor(isSelected? pane.getBackgroundAt(tabIndex) : unselectedTabBackground);
  161.         switch(tabPlacement) {
  162.           case LEFT:
  163.               g.fillRect(x+1, y+1, w-1, h-2);
  164.               break;
  165.           case RIGHT:
  166.               g.fillRect(x, y+1, w-1, h-2);
  167.               break;
  168.           case BOTTOM:
  169.               g.fillRect(x+1, y, w-2, h-3);
  170.               g.drawLine(x+2, y+h-3, x+w-3, y+h-3);
  171.               g.drawLine(x+3, y+h-2, x+w-4, y+h-2);
  172.               break;
  173.           case TOP:
  174.           default:
  175.               g.fillRect(x+1, y+3, w-2, h-3);
  176.               g.drawLine(x+2, y+2, x+w-3, y+2);
  177.               g.drawLine(x+3, y+1, x+w-4, y+1);
  178.         }
  179.  
  180.     }
  181.  
  182.     protected void paintTabBorder(Graphics g, JTabbedPane pane, 
  183.                                   int tabPlacement, int tabIndex,
  184.                                   int x, int y, int w, int h,
  185.                   boolean isSelected) { 
  186.         g.setColor(isSelected? tabHighlight : unselectedTabHighlight);
  187.  
  188.         switch(tabPlacement) {
  189.           case LEFT:
  190.               g.drawLine(x, y+2, x, y+h-3);
  191.               g.drawLine(x+1, y+1, x+1, y+2);
  192.               g.drawLine(x+2, y, x+2, y+1);
  193.               g.drawLine(x+3, y, x+w-1, y);
  194.               g.setColor(isSelected? tabShadow : unselectedTabShadow);
  195.               g.drawLine(x+1, y+h-3, x+1, y+h-2);
  196.               g.drawLine(x+2, y+h-2, x+2, y+h-1);
  197.               g.drawLine(x+3, y+h-1, x+w-1, y+h-1);
  198.               break;
  199.           case RIGHT:
  200.               g.drawLine(x, y, x+w-3, y);
  201.               g.setColor(isSelected? tabShadow : unselectedTabShadow);
  202.               g.drawLine(x+w-3, y, x+w-3, y+1);
  203.               g.drawLine(x+w-2, y+1, x+w-2, y+2);
  204.               g.drawLine(x+w-1, y+2, x+w-1, y+h-3);
  205.               g.drawLine(x+w-2, y+h-3, x+w-2, y+h-2);
  206.               g.drawLine(x+w-3, y+h-2, x+w-3, y+h-1);
  207.               g.drawLine(x, y+h-1, x+w-3, y+h-1);
  208.               break;
  209.           case BOTTOM:
  210.               g.drawLine(x, y, x, y+h-3);
  211.               g.drawLine(x+1, y+h-3, x+1, y+h-2);
  212.               g.drawLine(x+2, y+h-2, x+2, y+h-1);
  213.               g.setColor(isSelected? tabShadow : unselectedTabShadow);
  214.               g.drawLine(x+3, y+h-1, x+w-4, y+h-1);
  215.               g.drawLine(x+w-3, y+h-2, x+w-3, y+h-1);
  216.               g.drawLine(x+w-2, y+h-3, x+w-2, y+h-2);
  217.               g.drawLine(x+w-1, y, x+w-1, y+h-3);
  218.               break;
  219.           case TOP:
  220.           default:
  221.               g.drawLine(x, y+2, x, y+h-1);
  222.               g.drawLine(x+1, y+1, x+1, y+2);
  223.               g.drawLine(x+2, y, x+2, y+1);
  224.               g.drawLine(x+3, y, x+w-4, y);
  225.               g.setColor(isSelected? tabShadow : unselectedTabShadow);
  226.               g.drawLine(x+w-3, y, x+w-3, y+1);
  227.               g.drawLine(x+w-2, y+1, x+w-2, y+2);
  228.               g.drawLine(x+w-1, y+2, x+w-1, y+h-1);
  229.         }
  230.  
  231.     }
  232.  
  233.     protected void paintFocusIndicator(Graphics g, JTabbedPane pane, int tabPlacement,
  234.                                        Rectangle[] rects, int tabIndex, 
  235.                                        Rectangle iconRect, Rectangle textRect,
  236.                                        boolean isSelected) {
  237.         Rectangle tabRect = rects[tabIndex];
  238.         if (pane.hasFocus() && isSelected) {
  239.             int x, y, w, h;
  240.         g.setColor(focus);
  241.             switch(tabPlacement) {
  242.               case LEFT:
  243.                   x = tabRect.x + 3;
  244.                   y = tabRect.y + 3;
  245.                   w = tabRect.width - 6;
  246.                   h = tabRect.height - 7;
  247.                   break;
  248.               case RIGHT:
  249.                   x = tabRect.x + 2;
  250.                   y = tabRect.y + 3;
  251.                   w = tabRect.width - 6;
  252.                   h = tabRect.height - 7;
  253.                   break;
  254.               case BOTTOM:
  255.                   x = tabRect.x + 3;
  256.                   y = tabRect.y + 2;
  257.                   w = tabRect.width - 7;
  258.                   h = tabRect.height - 6;
  259.                   break;
  260.               case TOP:
  261.               default:
  262.                   x = tabRect.x + 3;
  263.                   y = tabRect.y + 3;
  264.                   w = tabRect.width - 7;
  265.                   h = tabRect.height - 6;
  266.             }
  267.             g.drawRect(x, y, w, h);
  268.         }
  269.     }
  270.  
  271.     protected void calculateLayoutInfo(JTabbedPane pane) {
  272.         int tabPlacement = pane.getTabPlacement();
  273.         Font paneFont = pane.getFont(); 
  274.         Font font = new Font(paneFont.getName(), 
  275.                              paneFont.getStyle() | Font.BOLD, 
  276.                              paneFont.getSize());         
  277.         int tabCount = pane.getTabCount();
  278.         int selectedIndex = pane.getSelectedIndex();
  279.         
  280.         assureRectsCreated(tabCount);
  281.  
  282.         arrangeTabs(pane, tabPlacement, tabCount, font); 
  283.  
  284.     padSelectedTab(pane, tabPlacement, selectedIndex);
  285.     }
  286.  
  287.     protected int getRunIndent(JTabbedPane pane, int tabPlacement, int run) {
  288.         return run*3;
  289.     }
  290.  
  291.     protected int getTabOverlay(JTabbedPane pane, int tabPlacement) {
  292.         overlay = (tabPlacement == LEFT || tabPlacement == RIGHT)?
  293.             (int)Math.round((float)maxTabWidth * .10) :
  294.             (int)Math.round((float)maxTabHeight * .22);
  295.         return overlay;
  296.     }
  297.         
  298.     protected Insets getTabAreaInsets(JTabbedPane pane, int tabPlacement) {
  299.         Insets insets;
  300.         switch(tabPlacement) {
  301.           case LEFT:
  302.               insets = mTabsOnLeftTabAreaInsets;
  303.               break;
  304.           case BOTTOM:
  305.               insets = mTabsOnBottomTabAreaInsets;
  306.               break;
  307.           case RIGHT:
  308.               insets = mTabsOnRightTabAreaInsets;
  309.               break;
  310.           case TOP:
  311.           default:
  312.               insets = mTabsOnTopTabAreaInsets;
  313.         }
  314.         return insets;
  315.     }
  316.  
  317.     protected Insets getContentBorderInsets(JTabbedPane pane, int tabPlacement) {
  318.         return mContentBorderInsets;
  319.     }
  320.        
  321. }
  322.